package com.infore.qualityControl.select.chain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.infore.qualityControl.select.filter.ConditionFilter;

public class ConditionFilterChain implements ConditionFilter {

    private List<ConditionFilter> filters = new ArrayList<ConditionFilter>();
    private int index = 0;

    public ConditionFilterChain addFilter(ConditionFilter filter) {
        filters.add(filter);
        return this;
    }

    @Override
    public void doFilter(List<Predicate> predicates, CriteriaBuilder criteriaBuilder, Root<?> root, String property, String operator,
            String content, ConditionFilterChain chain, boolean interrupt) {
        if (interrupt || index == filters.size()) {
            index = 0;
            return;
        }
        ConditionFilter filter = filters.get(index);
        index++;
        filter.doFilter(predicates, criteriaBuilder, root, property, operator, content, chain, interrupt);

    }

}
